require "Net/send_Msg"
require "Net/TitleHandler"
require 'View/UIBase/UIBase'

UIInvasionTeamList = UIBase.New(PanelNames.UIInvasionTeamList);
local clickRefreshTime={value=0}
function UIInvasionTeamList.Init(func)
    UIBase.Init(UIInvasionTeamList, func);
end

--定义控件
function UIInvasionTeamList:OnCreate()
    ---table<string,GComponent>--根据英雄id索引对应的列表item

    self.teamList = self:GetChild("list");--队伍列表
    self.refreshBtn = self:GetChild("n63");
    self.searchBtn = self:GetChild("btn_Find");--搜索按钮
    self.backBtn = self:GetChild("btnBack");--返回按钮
    self.bgCloseBtn = self:GetChild("bgclose");--点背景返回按钮
    self.btnHide = self:GetChild("n67")--隐藏申请队伍
    self.ctrlHide = self:GetController("state",self.btnHide)
    self.titleText = self:GetChild("title");
    self.searchInput = self:GetChild("InputField");
    self.nullCtrl=self:GetController("null")
    -------------init
    self.teamList.itemRenderer = function(index, obj)
        self:setData(index + 1, obj);
    end
    self.originTeamDataList = nil
    self.isHide = GetPlayerPrefs("InvasionTeamHide") == "1"
    self.teamList.numItems=0
    self.teamDatas={}
    self:addEvent();
end
function UIInvasionTeamList:OnEnable()
    self.RefreshFunc()
end

function UIInvasionTeamList:OnDisable()

end

function UIInvasionTeamList:OnDestroy()
    self.originTeamDataList = nil
    self.teamDatas = nil
end

-------------------------------function--------------------------------------

--初始化
function UIInvasionTeamList:initData()

end

function UIInvasionTeamList:RefreshTeamState(teamId,state) --state,0:可申请，1：申请中，2，不可申请，待移除
    local index=table.findIndexByJudge(self.originTeamDataList,function(value)
        return tostring(value.teamId) ==tostring(teamId)
    end)
    if index>0 then
        local teamData=self.originTeamDataList[index]
        if state==0 then
            teamData.state=0
        elseif state==1 then
            teamData.state=1
        elseif state==2 then
            table.remove(self.originTeamDataList,index)
        end
        self:RefreshUI()
    end
end
function UIInvasionTeamList.Sort(a,b)
    return a.bossLv > b.bossLv
end
function UIInvasionTeamList:Refresh(teamDatas)
    --self.teamDatas=teamDatas
    self.originTeamDataList = teamDatas
    for i,v in ipairs(self.originTeamDataList) do
        v.state=0
    end
    self:RefreshUI()
end

function UIInvasionTeamList:RefreshUI()
    self.ctrlHide.selectedIndex = self.isHide and 1 or 0
    if self.isHide then
        self.teamDatas = {}
        for k,v in pairs(self.originTeamDataList) do
            if v.setting.join == 1 then
                table.insert(self.teamDatas,v)
            end
        end
    else
        self.teamDatas = self.originTeamDataList
    end
    table.sort(self.teamDatas, UIInvasionTeamList.Sort)
    self.teamList.numItems=0
    self.teamList.numItems=#self.teamDatas
    if #self.teamDatas>0 then
        self.nullCtrl.selectedIndex=0
    else
        self.nullCtrl.selectedIndex=1
    end
end
function UIInvasionTeamList:setData(index, obj)
    local function SetHeadIcon(head,modelId)
        local temp=head:GetChild("head")
        local headIcon=temp:GetChild("HeadIcon")
        if modelId==nil then
            headIcon.url=EnumConst.AtlasPath.CommonUI.."icon_hero_kb91"
        else
            headIcon.url=logicMgr.SkinManager.GetHeroIconBySkinId(modelId)
        end
    end
    local InvasionLevelText = obj:GetChild("InvasionLevel");--入侵等级
    local captainNameText = obj:GetChild("captainName");--队长名称
    local captainNameItemCom = obj:GetChild("captainIcon");--队长头像
    local applyBtn = obj:GetChild("btnJoin");--申请加入队伍
    local stateCtrl = obj:GetController("state");--控制器状态
    local playerComTable = {}--其他三名队员头像

    local teamData=self.teamDatas[index]
    InvasionLevelText.text=string.format(getLanguage("InvasionRank"),teamData.bossLv)
    stateCtrl.selectedIndex=teamData.state
    applyBtn.onClick:Clear()
    applyBtn.onClick:Add(function()
        InvasionHandler.ReqJoinTeamBoss(teamData.teamId,0)
    end)
    local memberInfos=teamData.member
    if not table.isNullOrEmpty(memberInfos)then
        captainNameText.text=memberInfos[1].name
        SetHeadIcon(captainNameItemCom,memberInfos[1].headId)
        for i = 1, 3 do
            local item = obj:GetChild("player"..i);
            if(memberInfos[i+1]~=nil)then
                SetHeadIcon(item,memberInfos[i+1].headId)
            else
                SetHeadIcon(item)
            end
            table.insert(playerComTable,item);
        end
    end
end
---关闭页面
function UIInvasionTeamList:onCloseClick()
    closeUI(PanelNames.UIInvasionTeamList);
end

function UIInvasionTeamList:addEvent()
    self:RegisterOnClick(self.backBtn, self.onCloseClick);
    self:RegisterOnClick(self.bgCloseBtn, self.onCloseClick);
    self:RegisterOnClick(self.gotoBattleBtn, self.onCloseClick);
    self:RegisterOnClick(self.refreshBtn,self.OnClickRefresh)
    self:RegisterOnClick(self.searchBtn,self.OnClickSearch)
    self:RegisterOnClick(self.btnHide,self.ClickHide)

    self:RegisterEvent(Config.EventType.InvasionFreshRecommendTeam,self.Refresh)
    self:RegisterEvent(Config.EventType.InvasionFreshJoinTeam,self.RefreshTeamState)

end
function UIInvasionTeamList.RefreshFunc()
    InvasionHandler.ReqRecommandTeamBoss()
end
function UIInvasionTeamList:OnClickRefresh()
    local cd=G.dataTable["t_global"][tostring(484)].f_int_value
    local func=self.RefreshFunc
    addFuncCd(func,clickRefreshTime,cd,getLanguage("InvasionBtnCD"))
end

function UIInvasionTeamList:OnClickSearch()
    if string.IsNilOrEmpty(self.searchInput.text)then
        self:OnClickRefresh()
    else
        local num=tonumber(self.searchInput.text)
        if num then
            if logicMgr.HeroManager.CompareMineId(num) then
                UISysTips.AddMsg(getLanguage("InvasionSearchMine"))
            else
                InvasionHandler.ReqSearchTeamBoss(num)
            end
        else
            UISysTips.AddMsg(getLanguage("InvasionSearch"))
        end
    end
end
function UIInvasionTeamList:ClickHide()
    self.isHide = not self.isHide
    SetPlayerPrefs("InvasionTeamHide",self.isHide and 1 or 0)
    self:RefreshUI()
end

function UIInvasionTeamList:removeEvent()
    self.tabTypeCtrl.onChanged:Clear();
end

return UIInvasionTeamList;